home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Plotting / GnuTerm_1.1a / Source / next.trm < prev    next >
Text File  |  1995-07-19  |  38KB  |  1,302 lines

  1. /*
  2.  * $Id: next.trm,v 1.0 1995/07/09 13:21:37 ril $
  3.  *
  4.  */
  5.  
  6. /* GNUPLOT - next.trm */
  7. /*
  8.  * Copyright (C) 1991, 1992   
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *  
  20.  * This software  is provided "as is" without express or implied warranty.
  21.  * 
  22.  * This file is included by ../term.c.
  23.  *
  24.  * This terminal driver supports:
  25.  *     next
  26.  *
  27.  * AUTHORS
  28.  *  Robert Lutwak from Russell Lang's post.trm
  29.  *    with thanks to Nick Strobel who first appreciated the ease of DNEXT
  30.  * 
  31.  * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
  32.  *
  33.  * This terminal attempts to connect, via the NeXTstep Distributed
  34.  * Objects system, to the "gnuplotServer."  If there is no such
  35.  * service registered with the OS, the terminal attempts to fire
  36.  * up GnuTerm.app.  If the user has not set the environment variable
  37.  * GNUTERMPATH, the terminal uses the users ApplicationPaths Workspace
  38.  * dwrite to search for GnuTerm.app (Note:  this is usually something
  39.  * like ~/ApNEXT, ~/LocalApNEXT, ~/NextApNEXT, etc.).
  40.  * In order to use this filter, you MUST have GnuTerm.app installed
  41.  * on your system.
  42.  *
  43.  * Once connected to the server, this filter is basically Russell Lang's
  44.  * Postscript filter, except that the resultant postscript code 
  45.  * is sent, via the D.O. system, to GnuTerm.app, which manages 
  46.  * the windows which produce the postscript output on the screen.
  47.  * 
  48.  *
  49.  * Defaults are
  50.  * 'set term next dashed monochrome "Helvetica" 14'
  51.  *
  52.  * To change font to Times-Roman and font size to 20pts use 
  53.  * 'set term next "Times-Roman" 20'.
  54.  */
  55. #import <appkit/appkit.h>
  56. #import <remote/NXProxy.h>    /* setProtocolForProxy */
  57. #import <remote/NXConnection.h>    /* setProtocolForProxy */
  58. #import <machkit/NXNetNameServer.h>
  59. #import <machkit/senderIsInvalid.h>
  60. #import <stdarg.h>
  61.  
  62. #ifndef GOT_DRIVER_H
  63. #include "driver.h"
  64. #endif
  65.  
  66. #ifdef TERM_REGISTER
  67. register_term(next)
  68. #endif
  69.  
  70. #ifdef TERM_PROTO
  71. TERM_PUBLIC void NEXT_options __P((void));
  72. TERM_PUBLIC void NEXT_common_init __P((int uses_fonts, unsigned int xoff, unsigned int yoff, unsigned int xsize, unsigned int ysize, char **dict));
  73. TERM_PUBLIC void NEXT_init __P((void));
  74. TERM_PUBLIC void NEXT_graphics __P((void));
  75. TERM_PUBLIC void NEXT_text __P((void));
  76. TERM_PUBLIC void NEXT_reset __P((void));
  77. TERM_PUBLIC void NEXT_linetype __P((int linetype));
  78. TERM_PUBLIC void NEXT_move __P((unsigned int x, unsigned int y));
  79. TERM_PUBLIC void NEXT_vector __P((unsigned int x, unsigned int y));
  80. TERM_PUBLIC void NEXT_put_text __P((unsigned int x, unsigned int y, char *str));
  81. TERM_PUBLIC int NEXT_text_angle __P((int ang));
  82. TERM_PUBLIC int NEXT_justify_text __P((enum JUSTIFY mode));
  83. TERM_PUBLIC void NEXT_point __P((unsigned int x, unsigned int y, int number));
  84. TERM_PUBLIC int NEXT_set_font __P((char * font));
  85. TERM_PUBLIC char *NEXT_RememberFont __P((char *fname));
  86. TERM_PUBLIC void NEXT_set_pointsize __P((double size));
  87.  
  88.  
  89. #define NEXT_POINT_TYPES 8    /* div */
  90. #define NEXT_XOFF    25    /* page offset in pts */
  91. #define NEXT_YOFF    25
  92. #define NEXT_XMAX 7200
  93. #define NEXT_YMAX 5040
  94. #define NEXT_XLAST (NEXT_XMAX - 1)
  95. #define NEXT_YLAST (NEXT_YMAX - 1)
  96. #define NEXT_VTIC (NEXT_YMAX/80)
  97. #define NEXT_HTIC (NEXT_YMAX/80)
  98. #define NEXT_SC (10)                /* scale is 1pt = 10 units */
  99. #define    NEXT_LW (0.5*NEXT_SC)        /* linewidth = 0.5 pts */
  100. #define NEXT_VCHAR (14*NEXT_SC)        /* default is 14 point characters */
  101. #define NEXT_HCHAR (14*NEXT_SC*6/10)
  102.  
  103. #define GOT_NEXT_PROTO
  104. #endif
  105.  
  106.  
  107. #ifndef TERM_PROTO_ONLY
  108.  
  109. #ifdef TERM_BODY
  110.  
  111. @interface GnuTermDriver: Object <NXSenderIsInvalid>
  112. {
  113.     id server;
  114. }
  115.  
  116. - senderIsInvalid:(id)sender;
  117. - plot:(char *)PSstr;
  118. - init;
  119. @end
  120.  
  121. #define DEFAULTNEXTSIZE 10000
  122.  
  123. static id gnuTermAccess;        /* local object manages the D.O. connection */
  124.  
  125. static char *NEXTBuffer, *NEXTBufAt, *NEXTBufEnd;
  126. static int NEXTsize;
  127. static char NEXTTmpBuf[1000];
  128. static void NEXTPrintf(char *, ...);
  129.  
  130.  
  131. char NEXT_font[MAX_ID_LEN+1] = "Helvetica" ; /* name of font */
  132. int NEXT_fontsize = 14;                     /* size of font in pts */
  133. TBOOLEAN NEXT_color = FALSE;
  134. TBOOLEAN NEXT_solid = FALSE;           /*  use dashed lines */
  135.             /* Added by Robert Davis <davis@ecn.purdue.edu> */
  136. int NEXT_path_count=0;     /* count of lines in path */
  137. int NEXT_ang=0;            /* text angle */
  138. enum JUSTIFY NEXT_justify=LEFT;    /* text is flush left */
  139.  
  140. /* added by Matt Heffron <heffron@falstaff.css.beckman.com> */
  141. static TBOOLEAN NEXT_duplex_state = FALSE;
  142. static TBOOLEAN NEXT_duplex_option = FALSE;
  143.  
  144. static char GPFAR * GPFAR NEXT_header[] = {
  145. "/M {moveto} bind def\n",
  146. "/L {lineto} bind def\n",
  147. "/R {rmoveto} bind def\n",
  148. "/V {rlineto} bind def\n",
  149. "/vpt2 vpt 2 mul def\n",
  150. "/hpt2 hpt 2 mul def\n",
  151. /* flush left show */
  152. "/Lshow { currentpoint stroke M\n",
  153. "  0 vshift R show } def\n", 
  154. /* flush right show */
  155. "/Rshow { currentpoint stroke M\n",
  156. "  dup stringwidth pop neg vshift R show } def\n", 
  157. /* centred show */
  158. "/Cshow { currentpoint stroke M\n",
  159. "  dup stringwidth pop -2 div vshift R show } def\n", 
  160. /* Dash or Color Line */
  161. "/DL { Color {setrgbcolor Solid {pop []} if 0 setdash }\n",
  162. " {pop pop pop Solid {pop []} if 0 setdash} ifelse } def\n",
  163. /* Border Lines */
  164. "/BL { stroke gnulinewidth 2 mul setlinewidth } def\n",
  165. /* Axes Lines */
  166. "/AL { stroke gnulinewidth 2 div setlinewidth } def\n",
  167. /* Plot Lines */
  168. "/PL { stroke gnulinewidth setlinewidth } def\n",
  169. /* Line Types */
  170. "/LTb { BL [] 0 0 0 DL } def\n", /* border */
  171. "/LTa { AL [1 dl 2 dl] 0 setdash 0 0 0 setrgbcolor } def\n", /* axes */
  172. "/LT0 { PL [] 0 1 0 DL } def\n",
  173. "/LT1 { PL [4 dl 2 dl] 0 0 1 DL } def\n",
  174. "/LT2 { PL [2 dl 3 dl] 1 0 0 DL } def\n",
  175. "/LT3 { PL [1 dl 1.5 dl] 1 0 1 DL } def\n",
  176. "/LT4 { PL [5 dl 2 dl 1 dl 2 dl] 0 1 1 DL } def\n",
  177. "/LT5 { PL [4 dl 3 dl 1 dl 3 dl] 1 1 0 DL } def\n",
  178. "/LT6 { PL [2 dl 2 dl 2 dl 4 dl] 0 0 0 DL } def\n",
  179. "/LT7 { PL [2 dl 2 dl 2 dl 2 dl 2 dl 4 dl] 1 0.3 0 DL } def\n",
  180. "/LT8 { PL [2 dl 2 dl 2 dl 2 dl 2 dl 2 dl 2 dl 4 dl] 0.5 0.5 0.5 DL } def\n",
  181. /* Point (Round) */    /* Matt Heffron make it round */
  182. "/Pnt { stroke [] 0 setdash\n",
  183. "   gsave 1 setlinecap M 0 0 V stroke grestore } def\n",
  184.  
  185. /* Diamond */
  186. "/Dia { stroke [] 0 setdash 2 copy vpt add M\n",
  187. "  hpt neg vpt neg V hpt vpt neg V\n",
  188. "  hpt vpt V hpt neg vpt V closepath stroke\n",
  189. "  Pnt } def\n",
  190.  
  191. /* Plus */
  192. "/Pls { stroke [] 0 setdash vpt sub M 0 vpt2 V\n",
  193. "  currentpoint stroke M\n",
  194. "  hpt neg vpt neg R hpt2 0 V stroke\n",
  195. "  } def\n",
  196.  
  197. /* Box */
  198. "/Box { stroke [] 0 setdash 2 copy exch hpt sub exch vpt add M\n",
  199. "  0 vpt2 neg V hpt2 0 V 0 vpt2 V\n",
  200. "  hpt2 neg 0 V closepath stroke\n",
  201. "  Pnt } def\n",
  202.  
  203. /* Cross (X) */
  204. "/Crs { stroke [] 0 setdash exch hpt sub exch vpt add M\n",
  205. "  hpt2 vpt2 neg V currentpoint stroke M\n",
  206. "  hpt2 neg 0 R hpt2 vpt2 V stroke } def\n",
  207.  
  208. /* Triangle Up*/
  209. "/TriU { stroke [] 0 setdash 2 copy vpt 1.12 mul add M\n",
  210. "  hpt neg vpt -1.62 mul V\n",
  211. "  hpt 2 mul 0 V\n",
  212. "  hpt neg vpt 1.62 mul V closepath stroke\n",
  213. "  Pnt  } def\n",
  214.  
  215. /* Star */
  216. "/Star { 2 copy Pls Crs } def\n",
  217.  
  218. /* div added filed box */
  219. /* Filled Box */
  220. "/BoxF { stroke [] 0 setdash exch hpt sub exch vpt add M\n",
  221. "  0 vpt2 neg V  hpt2 0 V  0 vpt2 V\n",
  222. "  hpt2 neg 0 V  closepath fill } def\n",
  223.  
  224. /* div added filled triangle */
  225. /* Triangle Up, Filled */
  226. "/TriUF { stroke [] 0 setdash vpt 1.12 mul add M\n",
  227. "  hpt neg vpt -1.62 mul V\n",
  228. "  hpt 2 mul 0 V\n",
  229. "  hpt neg vpt 1.62 mul V closepath fill } def\n",
  230.  
  231. /* Matt Heffron: added a few more types */
  232. /* Triangle Down */
  233. "/TriD { stroke [] 0 setdash 2 copy vpt 1.12 mul sub M\n", 
  234. "  hpt neg vpt 1.62 mul V\n",
  235. "  hpt 2 mul 0 V\n",
  236. "  hpt neg vpt -1.62 mul V closepath stroke\n",
  237. "  Pnt  } def\n",
  238.  
  239. /* Triangle Down, Filled*/
  240. "/TriDF { stroke [] 0 setdash vpt 1.12 mul sub M\n",
  241. "  hpt neg vpt 1.62 mul V\n",
  242. "  hpt 2 mul 0 V\n",
  243. "  hpt neg vpt -1.62 mul V closepath fill} def\n",
  244.  
  245. /* Diamond, Filled */
  246. "/DiaF { stroke [] 0 setdash vpt add M\n",
  247. "  hpt neg vpt neg V hpt vpt neg V\n",
  248. "  hpt vpt V hpt neg vpt V closepath fill } def\n",
  249.  
  250. /* Pentagon */
  251. "/Pent { stroke [] 0 setdash 2 copy gsave\n",
  252. "  translate 0 hpt M 4 {72 rotate 0 hpt L} repeat\n",
  253. "  closepath stroke grestore Pnt } def\n",
  254.  
  255. /* Pentagon, Filled */
  256. "/PentF { stroke [] 0 setdash gsave\n",
  257. "  translate 0 hpt M 4 {72 rotate 0 hpt L} repeat\n",
  258. "  closepath fill grestore } def\n",
  259.  
  260. /* Circle */
  261. "/Circle { stroke [] 0 setdash 2 copy\n",
  262. "  hpt 0 360 arc stroke Pnt } def\n",
  263.  
  264. /* Circle,Filled */
  265. "/CircleF { stroke [] 0 setdash hpt 0 360 arc fill } def\n",
  266. /* 16 differently filled circles */
  267. "/C0 { BL [] 0 setdash 2 copy moveto vpt 90 450  arc } bind def\n",
  268. "/C1 { BL [] 0 setdash 2 copy        moveto\n",
  269. "       2 copy  vpt 0 90 arc closepath fill\n",
  270. "               vpt 0 360 arc closepath } bind def\n",
  271. "/C2 { BL [] 0 setdash 2 copy moveto\n",
  272. "       2 copy  vpt 90 180 arc closepath fill\n",
  273. "               vpt 0 360 arc closepath } bind def\n",
  274. "/C3 { BL [] 0 setdash 2 copy moveto\n",
  275. "       2 copy  vpt 0 180 arc closepath fill\n",
  276. "               vpt 0 360 arc closepath } bind def\n",
  277. "/C4 { BL [] 0 setdash 2 copy moveto\n",
  278. "       2 copy  vpt 180 270 arc closepath fill\n",
  279. "               vpt 0 360 arc closepath } bind def\n",
  280. "/C5 { BL [] 0 setdash 2 copy moveto\n",
  281. "       2 copy  vpt 0 90 arc\n",
  282. "       2 copy moveto\n",
  283. "       2 copy  vpt 180 270 arc closepath fill\n",
  284. "               vpt 0 360 arc } bind def\n",
  285. "/C6 { BL [] 0 setdash 2 copy moveto\n",
  286. "      2 copy  vpt 90 270 arc closepath fill\n",
  287. "              vpt 0 360 arc closepath } bind def\n",
  288. "/C7 { BL [] 0 setdash 2 copy moveto\n",
  289. "      2 copy  vpt 0 270 arc closepath fill\n",
  290. "              vpt 0 360 arc closepath } bind def\n",
  291. "/C8 { BL [] 0 setdash 2 copy moveto\n",
  292. "      2 copy vpt 270 360 arc closepath fill\n",
  293. "              vpt 0 360 arc closepath } bind def\n",
  294. "/C9 { BL [] 0 setdash 2 copy moveto\n",
  295. "      2 copy  vpt 270 450 arc closepath fill\n",
  296. "              vpt 0 360 arc closepath } bind def\n",
  297. "/C10 { BL [] 0 setdash 2 copy 2 copy moveto vpt 270 360 arc closepath fill\n",
  298. "       2 copy moveto\n",
  299. "       2 copy vpt 90 180 arc closepath fill\n",
  300. "               vpt 0 360 arc closepath } bind def\n",
  301. "/C11 { BL [] 0 setdash 2 copy moveto\n",
  302. "       2 copy  vpt 0 90 arc closepath fill\n",
  303. "       2 copy moveto\n",
  304. "       2 copy  vpt 180 360 arc closepath fill\n",
  305. "               vpt 0 360 arc closepath } bind def\n",
  306. "/C12 { BL [] 0 setdash 2 copy moveto\n",
  307. "       2 copy  vpt 180 360 arc closepath fill\n",
  308. "               vpt 0 360 arc closepath } bind def\n",
  309. "/C13 { BL [] 0 setdash  2 copy moveto\n",
  310. "       2 copy  vpt 0 90 arc closepath fill\n",
  311. "       2 copy moveto\n",
  312. "       2 copy  vpt 180 360 arc closepath fill\n",
  313. "               vpt 0 360 arc closepath } bind def\n",
  314. "/C14 { BL [] 0 setdash 2 copy moveto\n",
  315. "       2 copy  vpt 90 360 arc closepath fill\n",
  316. "               vpt 0 360 arc } bind def\n",
  317. "/C15 { BL [] 0 setdash 2 copy vpt 0 360 arc closepath fill\n",
  318. "               vpt 0 360 arc closepath } bind def\n",
  319.  
  320. /* Auxiliary definitions for rectangles */
  321.  
  322. "/Rec   { newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n",
  323. "       neg 0 rlineto closepath } bind def\n",
  324. "/Square { dup Rec } bind def\n",
  325. "/Bsquare { vpt sub exch vpt sub exch vpt2 Square } bind def\n",
  326.  
  327. /* 16 differently filled squares */
  328.  
  329. "/S0 { BL [] 0 setdash 2 copy moveto 0 vpt rlineto BL Bsquare } bind def\n",
  330. "/S1 { BL [] 0 setdash 2 copy vpt Square fill Bsquare } bind def\n",
  331. "/S2 { BL [] 0 setdash 2 copy exch vpt sub exch vpt Square fill Bsquare } bind def\n",
  332. "/S3 { BL [] 0 setdash 2 copy exch vpt sub exch vpt2 vpt Rec fill Bsquare } bind def\n",
  333. "/S4 { BL [] 0 setdash 2 copy exch vpt sub exch vpt sub vpt Square fill Bsquare } bind def\n",
  334. "/S5 { BL [] 0 setdash 2 copy 2 copy vpt Square fill\n",
  335. "       exch vpt sub exch vpt sub vpt Square fill Bsquare } bind def\n",
  336. "/S6 { BL [] 0 setdash 2 copy exch vpt sub exch vpt sub vpt vpt2 Rec fill Bsquare } bind def\n",
  337. "/S7 { BL [] 0 setdash 2 copy exch vpt sub exch vpt sub vpt vpt2 Rec fill\n",
  338. "       2 copy vpt Square fill\n",
  339. "       Bsquare } bind def\n",
  340. "/S8 { BL [] 0 setdash 2 copy vpt sub vpt Square fill Bsquare } bind def\n",
  341. "/S9 { BL [] 0 setdash 2 copy vpt sub vpt vpt2 Rec fill Bsquare } bind def\n",
  342. "/S10 { BL [] 0 setdash 2 copy vpt sub vpt Square fill 2 copy exch vpt sub exch vpt Square fill\n",
  343. "       Bsquare } bind def\n",
  344. "/S11 { 2 copy vpt sub vpt Square fill 2 copy exch vpt sub exch vpt2 vpt Rec fill\n",
  345. "       Bsquare } bind def\n",
  346. "/S12 { BL [] 0 setdash 2 copy exch vpt sub exch vpt sub vpt2 vpt Rec fill Bsquare } bind def\n",
  347. "/S13 { BL [] 0 setdash 2 copy exch vpt sub exch vpt sub vpt2 vpt Rec fill\n",
  348. "       2 copy vpt Square fill Bsquare } bind def\n",
  349. "/S14 { BL [] 0 setdash 2 copy exch vpt sub exch vpt sub vpt2 vpt Rec fill\n",
  350. "       2 copy exch vpt sub exch vpt Square fill Bsquare } bind def\n",
  351. "/S15 { BL [] 0 setdash 2 copy Bsquare fill Bsquare } bind def\n",
  352.  
  353. /* 16 different diamonds (actually just rotated squares) */
  354.  
  355. "/D0 { gsave translate 45 rotate 0 0 Box stroke grestore } bind def\n",
  356. "/D1 { gsave translate 45 rotate 0 0 S1 stroke grestore } bind def\n",
  357. "/D2 { gsave translate 45 rotate 0 0 S2 stroke grestore } bind def\n",
  358. "/D3 { gsave translate 45 rotate 0 0 S3 stroke grestore } bind def\n",
  359. "/D4 { gsave translate 45 rotate 0 0 S4 stroke grestore } bind def\n",
  360. "/D5 { gsave translate 45 rotate 0 0 S5 stroke grestore } bind def\n",
  361. "/D6 { gsave translate 45 rotate 0 0 S6 stroke grestore } bind def\n",
  362. "/D7 { gsave translate 45 rotate 0 0 S7 stroke grestore } bind def\n",
  363. "/D8 { gsave translate 45 rotate 0 0 S8 stroke grestore } bind def\n",
  364. "/D9 { gsave translate 45 rotate 0 0 S9 stroke grestore } bind def\n",
  365. "/D10 { gsave translate 45 rotate 0 0 S10 stroke grestore } bind def\n",
  366. "/D11 { gsave translate 45 rotate 0 0 S11 stroke grestore } bind def\n",
  367. "/D12 { gsave translate 45 rotate 0 0 S12 stroke grestore } bind def\n",
  368. "/D13 { gsave translate 45 rotate 0 0 S13 stroke grestore } bind def\n",
  369. "/D14 { gsave translate 45 rotate 0 0 S14 stroke grestore } bind def\n",
  370. "/D15 { gsave translate 45 rotate 0 0 S15 stroke grestore } bind def\n",
  371.  
  372. NULL
  373. };
  374.  
  375. static char GPFAR * GPFAR ENHNEXT_header[] = {
  376. /* For MFshow and MFwidth the tos is an array with the string and font info:  */
  377. /*    [<fontname (a string)> <fontsize> <vertical offset> <width significant?> <text string>]  */
  378.  
  379. "/MFshow {{dup dup 0 get findfont exch 1 get scalefont setfont\n",
  380. "     [ currentpoint ] exch dup 2 get 0 exch rmoveto dup 4 get show dup\n",
  381. "     3 get {2 get neg 0 exch rmoveto pop} {pop aload pop moveto}ifelse} forall} bind def\n",
  382. "/MFwidth {0 exch {dup 3 get{dup dup 0 get findfont exch 1 get scalefont setfont\n",
  383. "      4 get stringwidth pop add}\n",
  384. "    {pop} ifelse} forall} bind def\n",
  385.  
  386. /* flush left show */
  387. "/MLshow { currentpoint stroke M\n",
  388. "  0 exch R MFshow } bind def\n", 
  389.  
  390. /* flush right show */
  391. "/MRshow { currentpoint stroke M\n",
  392. "  exch dup MFwidth neg 3 -1 roll R MFshow } def\n", 
  393.  
  394. /* centred show */
  395. "/MCshow { currentpoint stroke M\n",
  396. "  exch dup MFwidth -2 div 3 -1 roll R MFshow } def\n", 
  397. NULL
  398. };
  399.  
  400. /* added to post by Matt Heffron <heffron@falstaff.css.beckman.com> */
  401. /* moved to post.trm by drd */
  402.  
  403. struct NEXT_FontName {
  404.     char *name;
  405.     struct NEXT_FontName *next;
  406. } *NEXT_DocFonts = NULL;
  407.  
  408. TERM_PUBLIC char *NEXT_RememberFont(fname)
  409. char *fname;
  410. {
  411.     struct NEXT_FontName *fnp;
  412.  
  413.    for (fnp=NEXT_DocFonts; fnp && strcmp(fnp->name, fname); fnp = fnp->next);
  414.    if (fnp)
  415.       return fnp->name;    /* we must have found it in the list */
  416.    
  417.    if (encoding == ENCODING_ISO_8859_1) {
  418.     NEXTPrintf("/%s reencodeISO def\n",fname);
  419.    }
  420.    fnp = (struct NEXT_FontName *)alloc(sizeof(struct NEXT_FontName), "PostScript Font record");
  421.    fnp->name = alloc(1+strlen(fname), "PostScript Font name");
  422.    strcpy(fnp->name, fname);
  423.    fnp->next = NEXT_DocFonts;
  424.    NEXT_DocFonts = fnp;
  425.    return fnp->name;
  426. }
  427.  
  428. int NEXT_pen_x, NEXT_pen_y;
  429. int NEXT_taken;
  430. int NEXT_linetype_last;
  431. TBOOLEAN NEXT_relative_ok;
  432.  
  433. TERM_PUBLIC void NEXT_options()
  434. {
  435.     if (!END_OF_COMMAND) {
  436.         if (almost_equals(c_token,"d$efault")) {
  437.             NEXT_color=FALSE;
  438.             NEXT_solid=FALSE;
  439.             NEXT_duplex_option=FALSE;
  440.             strcpy(NEXT_font,"Helvetica");
  441.             NEXT_fontsize = 14;
  442.             term->v_char = (unsigned int)(NEXT_fontsize*NEXT_SC);
  443.             term->h_char = (unsigned int)(NEXT_fontsize*NEXT_SC*6/10);
  444.             term->put_text = NEXT_put_text;
  445.             c_token++;
  446.         }
  447.     }
  448.  
  449.  
  450.     if (!END_OF_COMMAND) {
  451.         if (almost_equals(c_token,"m$onochrome")) {
  452.             NEXT_color=FALSE;
  453.             c_token++;
  454.         }
  455.         else if (almost_equals(c_token,"c$olor")) {
  456.             NEXT_color=TRUE;
  457.             c_token++;
  458.         }
  459.     }
  460.  
  461.     if (!END_OF_COMMAND) {
  462.         if (almost_equals(c_token,"so$lid")) {
  463.             NEXT_solid=TRUE;
  464.             c_token++;
  465.         }
  466.         else if (almost_equals(c_token,"da$shed")) {
  467.             NEXT_solid=FALSE;
  468.             c_token++;
  469.         }
  470.     }
  471.  
  472.     if (!END_OF_COMMAND) {
  473.         if (almost_equals(c_token,"si$mplex")) {
  474.             NEXT_duplex_state  = FALSE;
  475.             NEXT_duplex_option = TRUE;
  476.             c_token++;
  477.         }
  478.         else if (almost_equals(c_token,"du$plex")) {
  479.             NEXT_duplex_state  = TRUE;
  480.             NEXT_duplex_option = TRUE;
  481.             c_token++;
  482.         }
  483.         else if (almost_equals(c_token,"defaultp$lex")) {
  484.             NEXT_duplex_option = FALSE;
  485.             c_token++;
  486.         }
  487.     }
  488.  
  489.     if (!END_OF_COMMAND && isstring(c_token)) {
  490.         quote_str(NEXT_font,c_token, MAX_ID_LEN);
  491.         c_token++;
  492.     }
  493.  
  494.     if (!END_OF_COMMAND) {
  495.         /* We have font size specified */
  496.         struct value a;
  497.         NEXT_fontsize = (int)real(const_express(&a));
  498.         term->v_char = (unsigned int)(NEXT_fontsize*NEXT_SC);
  499.         term->h_char = (unsigned int)(NEXT_fontsize*NEXT_SC*6/10);
  500.     }
  501.  
  502.     sprintf(default_font,"%s,%d",NEXT_font,NEXT_fontsize);
  503.     /* default_font holds the font and size set at 'set term' */
  504.     /* Entry font added by DJL */
  505.     sprintf(term_options,"%s %s %s \"%s\" %d",
  506.         NEXT_color ? "color" : "monochrome",
  507.         NEXT_solid ? "solid" : "dashed",
  508.         NEXT_duplex_option ? (NEXT_duplex_state ? "duplex" : "simplex")
  509.                                     : "defaultplex",
  510.         NEXT_font,NEXT_fontsize);
  511. }
  512.  
  513. /* store settings passed to common_init() for use in NEXT_graphics()
  514.  * are reserved for storing the term options
  515.  */
  516. static int NEXT_common_uses_fonts;
  517. static unsigned int NEXT_common_xoff, NEXT_common_yoff;
  518.  
  519.  
  520. TERM_PUBLIC void NEXT_common_init(uses_fonts, xoff, yoff, width, height, dict)
  521. int uses_fonts; /* 0 for NEXT(la)tex */
  522. unsigned int xoff, yoff; /* offset  - 50 for /post, 0 for NEXT(la)tex */
  523. unsigned int width,height; /* for bounding box */
  524. char **dict; /* extra entries for the dictionary */
  525. {
  526. static char GPFAR NEXTi1[] = "%%%%Creator: gnuplot\n\
  527. %%%%DocumentFonts: %s\n";
  528. static char GPFAR NEXTi2[] = "%%%%EndComments\n\
  529. /gnudict 120 dict def\ngnudict begin\n\
  530. /Color %s def\n\
  531. /Solid %s def\n\
  532. /gnulinewidth %.3f def\n\
  533. /vshift %d def\n\
  534. /dl {%d mul} def\n\
  535. /hpt %.1f def\n\
  536. /vpt %.1f def\n";
  537. static char GPFAR *NEXT_iso_8859_1_encoding[] = {
  538. "/reencodeISO {\n",
  539. "dup dup findfont dup length dict begin\n",
  540. "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n",
  541. "/Encoding ISOLatin1Encoding def\n",
  542. "currentdict end definefont\n",
  543. "} def\n",
  544. "/ISOLatin1Encoding [\n",
  545. "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n",
  546. "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n",
  547. "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n",
  548. "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n",
  549. "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n",
  550. "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n",
  551. "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n",
  552. "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n",
  553. "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n",
  554. "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n",
  555. "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n",
  556. "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n",
  557. "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n",
  558. "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n",
  559. "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n",
  560. "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n",
  561. "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n",
  562. "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n",
  563. "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n",
  564. "/guillemotright/onequarter/onehalf/threequarters/questiondown\n",
  565. "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n",
  566. "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n",
  567. "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n",
  568. "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n",
  569. "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n",
  570. "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n",
  571. "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n",
  572. "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n",
  573. "/yacute/thorn/ydieresis\n",
  574. "] def\n",
  575. NULL };
  576.  
  577.  
  578.     struct termentry *t = term;
  579.     int i;
  580.  
  581.         NEXT_common_uses_fonts = uses_fonts;
  582.     NEXT_common_xoff = xoff;
  583.     NEXT_common_yoff = yoff;
  584.  
  585.  
  586.     NEXTPrintf("%%!NEXT-Adobe-2.0\n");
  587.         
  588.     NEXTPrintf( NEXTi1, uses_fonts ? "(atend)" : "");
  589.  
  590.     NEXTPrintf("%%%%BoundingBox: %d %d %d %d\n", xoff, yoff,
  591.         (int)(xsize*width/NEXT_SC+0.5+xoff), 
  592.         (int)(ysize*height/NEXT_SC+0.5+yoff) );
  593.  
  594.     NEXTPrintf( NEXTi2,
  595.         NEXT_color ? "true" : "false",
  596.         NEXT_solid ? "true" : "false",
  597.         NEXT_LW,            /* line width */
  598.          (int)(t->v_char)/(-3),    /* shift for vertical centring */
  599.         NEXT_SC,            /* dash length */
  600.         pointsize*NEXT_HTIC/2.0,        /* half point width */
  601.         pointsize*NEXT_VTIC/2.0);        /* half point height */
  602.  
  603.     if (uses_fonts && (encoding == ENCODING_ISO_8859_1)) {
  604.         for (i=0; NEXT_iso_8859_1_encoding[i] != NULL; i++) {
  605.             NEXTPrintf("%s",NEXT_iso_8859_1_encoding[i]);
  606.         }
  607.     }
  608.     for ( i=0; NEXT_header[i] != NULL; i++)
  609.         NEXTPrintf("%s",NEXT_header[i]);
  610.     if (NEXT_duplex_option)
  611.         NEXTPrintf( "statusdict begin %s setduplexmode end\n",
  612.                 NEXT_duplex_state ? "true" : "false");
  613.     NEXT_RememberFont(NEXT_font);
  614.  
  615.     if (dict)
  616.         while (*dict)
  617.             NEXTPrintf("%s", *(dict++));    
  618.  
  619.     NEXTPrintf("end\n%%%%EndProlog\n");
  620. }
  621.  
  622. /* the init fn for the postscript driver */
  623. TERM_PUBLIC void NEXT_init()
  624. {
  625.  
  626.                 /* Connect to server port */
  627.     gnuTermAccess = [[GnuTermDriver alloc] init];
  628.  
  629.                     /* Initialize output string */
  630.     NEXTsize = DEFAULTNEXTSIZE;
  631.     if ((NEXTBuffer = malloc(NEXTsize)) == NULL) {
  632.         printf("Malloc error in next filter init\n");
  633.         exit(1);
  634.     }
  635.     NEXTBufEnd = NEXTBuffer + NEXTsize;
  636.     NEXTBufAt = NEXTBuffer;
  637.  
  638.     term->xmax = NEXT_XMAX;
  639.     term->ymax = NEXT_YMAX;
  640. /*    NEXT_common_init(1, NEXT_XOFF, NEXT_YOFF, term->xmax, term->ymax,
  641.        NEXT_header);*/
  642. }
  643.  
  644.  
  645. TERM_PUBLIC void NEXT_graphics()
  646. {
  647. static char GPFAR NEXTg1[] = "0 setgray\nnewpath\n";
  648. struct termentry *t = term;
  649.  
  650.  
  651.     *NEXTBuffer = 0;
  652.     NEXTBufAt = NEXTBuffer;
  653.  
  654. NEXT_common_init(1, NEXT_XOFF, NEXT_YOFF, term->xmax, term->ymax,
  655.        ENHNEXT_header);
  656.  
  657.     NEXTPrintf("gnudict begin\ngsave\n");
  658.     NEXTPrintf("%d %d translate\n",NEXT_common_xoff, NEXT_common_yoff);
  659.     NEXTPrintf("%.3f %.3f scale\n", 1.0/NEXT_SC,1.0/NEXT_SC);
  660.     
  661. /*    NEXTPrintf("90 rotate\n0 %d translate\n", (int)(-NEXT_YMAX)); */
  662.  
  663.     NEXTPrintf( NEXTg1);
  664.     if (NEXT_common_uses_fonts)
  665.         NEXTPrintf( "(%s) findfont %d scalefont setfont\n", NEXT_font, (t->v_char) );
  666.     NEXT_path_count = 0;
  667.     NEXT_relative_ok = FALSE;
  668.     NEXT_pen_x = NEXT_pen_y = -4000;
  669.     NEXT_taken = 0;
  670.     NEXT_linetype_last = -1;
  671. }
  672.  
  673.  
  674. TERM_PUBLIC void NEXT_text()
  675. {
  676.     NEXT_path_count = 0;
  677.     NEXTPrintf("stroke\ngrestore\nend\n");
  678.     
  679.                     /* Terminate string */
  680.     *NEXTBufAt = 0;
  681.  
  682.                     /* Here's the call that dumps the string to the server */
  683.     [gnuTermAccess plot:NEXTBuffer];
  684.  
  685. }
  686.  
  687.  
  688. TERM_PUBLIC void NEXT_reset()
  689. {
  690.     NEXTPrintf("%%%%Trailer\n");
  691.     if (!NEXT_common_uses_fonts) {
  692.         NEXTPrintf("%%%%DocumentFonts: ");
  693.         while (NEXT_DocFonts) {
  694.             struct NEXT_FontName *fnp;
  695.             fnp = NEXT_DocFonts->next;
  696.             NEXTPrintf( "%s%s", NEXT_DocFonts->name, fnp ? ", " : "\n");
  697.             free(NEXT_DocFonts->name);
  698.             free(NEXT_DocFonts);
  699.             NEXT_DocFonts=fnp;
  700.         }
  701.     }
  702. }
  703.  
  704.  
  705. TERM_PUBLIC void NEXT_linetype(linetype)
  706. int linetype;
  707. {
  708.     linetype = (linetype % 9) + 2;
  709.     if (linetype < 0) linetype = 0;
  710.     NEXT_relative_ok = FALSE;
  711.     if (NEXT_linetype_last == linetype) return;
  712.     NEXT_linetype_last = linetype;
  713.     NEXTPrintf("LT%c\n", "ba012345678"[linetype]);
  714.     NEXT_path_count = 0;
  715. }
  716.  
  717.  
  718. TERM_PUBLIC void NEXT_move(x,y)
  719. unsigned int x,y;
  720. {
  721.     int dx, dy;
  722.     char abso[20],rel[20];
  723.     dx = x - NEXT_pen_x;
  724.     dy = y - NEXT_pen_y;
  725.     /* can't cancel all null moves--need a move after stroke'ing */
  726.     if (dx==0 && dy==0 && NEXT_relative_ok)
  727.         return;
  728.     sprintf(abso, "%d %d M\n", x, y);
  729.     sprintf(rel, "%d %d R\n", dx, dy);
  730.     if (strlen(rel) < strlen(abso) && NEXT_relative_ok){
  731.         NEXTPrintf("%s", rel);
  732.         NEXT_taken++;
  733.     }else
  734.         NEXTPrintf("%s", abso);
  735.     NEXT_relative_ok = TRUE;
  736.     NEXT_path_count += 1;
  737.  
  738.     NEXT_pen_x = x;
  739.     NEXT_pen_y = y;
  740. }
  741.  
  742. TERM_PUBLIC void NEXT_vector(x,y)
  743. unsigned int x,y;
  744. {
  745.     int dx, dy;
  746.     char abso[20],rel[20];
  747.     dx = x - NEXT_pen_x;
  748.     dy = y - NEXT_pen_y;
  749.     if (dx==0 && dy==0) return;
  750.     sprintf(abso, "%d %d L\n", x, y);
  751.     sprintf(rel, "%d %d V\n", dx, dy);
  752.     if (strlen(rel) < strlen(abso) && NEXT_relative_ok){
  753.         NEXTPrintf("%s", rel);
  754.         NEXT_taken++;
  755.     }else
  756.         NEXTPrintf("%s", abso);
  757.     NEXT_relative_ok = TRUE;
  758.     NEXT_path_count += 1;
  759.     NEXT_pen_x = x;
  760.     NEXT_pen_y = y;
  761.     if (NEXT_path_count >= 400) {
  762.         NEXTPrintf("currentpoint stroke M\n");
  763.         NEXT_path_count = 0;
  764.     }
  765. }
  766.  
  767.  
  768.  
  769.  
  770. TERM_PUBLIC int NEXT_text_angle(ang)
  771. int ang;
  772. {
  773.     NEXT_ang=ang;
  774.     return TRUE;
  775. }
  776.  
  777.  
  778. TERM_PUBLIC int NEXT_justify_text(mode)
  779. enum JUSTIFY mode;
  780. {
  781.     NEXT_justify=mode;
  782.     return TRUE;
  783. }
  784.  
  785.  
  786. TERM_PUBLIC int NEXT_set_font(font)  /* Entry font added by DJL */
  787. char *font;
  788. {
  789. char name[32];
  790. int  size,sep;
  791.  
  792.         sep=strcspn(font,",");
  793.         strncpy(name,font,sep); name[sep]='\0';
  794.         size=NEXT_fontsize; sscanf (&(font[sep+1]),"%d",&size);
  795.         NEXTPrintf("/%s findfont %d scalefont setfont\n",name,size*NEXT_SC);
  796.         NEXT_RememberFont(name);
  797.         return TRUE;
  798. }
  799.  
  800.  
  801. /* postscript point routines */
  802.  
  803. TERM_PUBLIC void NEXT_set_pointsize(size)
  804. double size;
  805. {
  806.         NEXTPrintf( "/vpt %.1f def /hpt %.1f def /vpt2 vpt 2 mul def /hpt2 hpt 2 mul def\n",
  807.            pointsize*NEXT_VTIC*0.5, pointsize*NEXT_HTIC*0.5);
  808. }
  809.  
  810. TERM_PUBLIC void NEXT_point(x,y,number)
  811. unsigned int x,y;
  812. int number;
  813. {
  814. static char *pointFNS[] = {"Pnt",  "Pls",   "Crs",    "Star",
  815.                            "Box",  "BoxF",  "Circle", "CircleF",
  816.                            "TriU", "TriUF", "TriD",   "TriDF",
  817.                            "Dia",  "DiaF",  "Pent",   "PentF",
  818.                    "C0",   "C1",    "C2",     "C3",
  819.                "C4",   "C5",    "C6",     "C7",
  820.                      "C8",   "C9",    "C10",    "C11",
  821.                "C12",  "C13",   "C14",    "C15",
  822.                            "S0",   "S1",    "S2",     "S3",
  823.                "S4",   "S5",    "S6",     "S7",
  824.                            "S8",   "S9",    "S10",    "S11",
  825.                "S12",  "S13",   "S14",    "S15",
  826.                      "D0",   "D1",    "D2",     "D3",
  827.                "D4",   "D5",    "D6",     "D7",
  828.                "D8",   "D9",    "D10",    "D11",
  829.                "D12",  "D13",   "D14",    "D15"
  830. };
  831.     if (number < 0)
  832.         number = -1;        /* negative types are all 'dot' */
  833.     else
  834.         number %= sizeof(pointFNS)/sizeof(pointFNS[0]) -1;
  835.     NEXTPrintf("%d %d %s\n", x, y, pointFNS[number+1]);
  836.  
  837.     NEXT_relative_ok = 0;
  838.     NEXT_path_count = 0;
  839.     NEXT_linetype_last = -1; /* force next linetype change */
  840. }
  841.  
  842.  
  843.  
  844.  
  845. /* All lifted from the anced postscript driver */
  846.  
  847.  
  848.  
  849. static TBOOLEAN NEXT_opened_string;  /* try to cut out empty ()'s */
  850.  
  851. /* used in determining height of processed text */
  852.  
  853. static float NEXT_max_height, NEXT_min_height;
  854.  
  855.  
  856. /* process a bit of string, and return the last character used.
  857.  * p is start of string
  858.  * brace is TRUE to keep processing to }, FALSE for do one character
  859.  * fontname & fontsize are obvious
  860.  * base is the current baseline
  861.  * widthflag is TRUE if the width of this should count,
  862.  *              FALSE for zero width boxes
  863.  */
  864.  
  865. static char *NEXT_recurse(p, brace, fontname, fontsize, base, widthflag)
  866. char *p, *fontname;
  867. TBOOLEAN brace, widthflag;
  868. double fontsize, base;
  869. {
  870.  
  871. /* close a postscript string if it has been opened */
  872. #define NEXT_FLUSH      \
  873. {    if (NEXT_opened_string)  \
  874.     {    NEXTPrintf("%s", ")]\n");   \
  875.         NEXT_opened_string = FALSE; \
  876.     }                         \
  877. }
  878.  
  879. #define NEXT_OPEN    \
  880. {    if (!NEXT_opened_string) \
  881.     { NEXTPrintf( "[(%s) %.1f %.1f %s (",  \
  882.           fontname, fontsize, base, \
  883.           widthflag ? "true" : "false");  \
  884.       NEXT_opened_string = TRUE; \
  885.     }    \
  886. }
  887.  
  888.     
  889.     /* Start each recursion with a clean string */
  890.     NEXT_FLUSH
  891.  
  892.     if (base + fontsize > NEXT_max_height)
  893.     {    NEXT_max_height = base + fontsize;
  894.     }
  895.  
  896.     if (base < NEXT_min_height)
  897.     {    NEXT_min_height = base;
  898.     }
  899.  
  900.     for ( ; *p; ++p)
  901.     {    float shift;
  902.         float f=0;   /* used for getting new font size */
  903.         char *localfontname, ch;
  904.  
  905.         switch (*p)
  906.         {
  907.             case '}'  :
  908.                 if (brace)
  909.                     return (p);
  910.                 
  911.                 fprintf(stderr, "next driver - spurious }\n");
  912.                 break;
  913.         
  914.             case '_'  :
  915.             case '^'  :
  916.                 shift = (*p == '^') ? 0.5 : -0.3;
  917.                 
  918.                 NEXT_FLUSH
  919.                 
  920.                 p = NEXT_recurse(p+1, FALSE, fontname, fontsize*0.8, base+shift*fontsize, widthflag);
  921.                 
  922.                 break;
  923.         
  924.             case '{'  :
  925.                 /*{{{  recurse (possibly with a new font) */
  926.                 
  927.                 if (*++p == '/')
  928.                 {    /* then parse a fontname, optional fontsize */
  929.                     while (*++p == ' ');
  930.                     localfontname = p;
  931.                     while ((ch = *p) > ' ' && ch != '=')
  932.                         ++p;
  933.                     if (ch == '=')
  934.                     {
  935.                         *p++ = '\0';                
  936.                         /*{{{  get optional font size*/
  937.                         f = (float)strtod(p, &p);
  938.                   
  939.                         if (f)
  940.                             f *= NEXT_SC;  /* remember the scaling */
  941.                         else
  942.                             f = fontsize;
  943.                   
  944.                         /*}}}*/
  945.                     }
  946.                     else
  947.                     {
  948.                         *p++ = '\0';
  949.                         f = fontsize;
  950.                     }                
  951.                 
  952.                     while (*p == ' ')
  953.                         ++p;
  954.                     if (*localfontname)
  955.                         localfontname = NEXT_RememberFont(localfontname);
  956.                     else
  957.                         localfontname = fontname;
  958.                 }
  959.                 else
  960.                 {
  961.                     localfontname = fontname;
  962.                     f = fontsize;
  963.                 }
  964.                 /*}}}*/
  965.                 
  966.                 
  967.                 p = NEXT_recurse(p, TRUE, localfontname, f, base, widthflag);
  968.                 
  969.                 
  970.                 NEXT_FLUSH
  971.         
  972.                 break;
  973.                 
  974.             case '@' :
  975.                 /*{{{  phantom box - prints next 'char', then restores currentpoint */
  976.     
  977.                 NEXT_FLUSH
  978.     
  979.                 p = NEXT_recurse(++p, FALSE, fontname, fontsize, base, FALSE);
  980.                     
  981.                 break;
  982.                 /*}}}*/
  983.         
  984.             case '('  :
  985.             case ')'  :
  986.                 /* special cases */
  987.                 NEXT_OPEN
  988.                 NEXTPrintf("\\");
  989.                 NEXTPrintf("%c",*p);
  990.                 break;
  991.                 /*}}}*/
  992.         
  993.             case '\\'  :
  994.                 /*{{{  is it an escape */
  995.                 /* special cases */
  996.                 
  997.                 if (p[1]=='\\' || p[1]=='(' || p[1]==')')
  998.                 {
  999.                     NEXT_OPEN
  1000.                     NEXTPrintf("%c", '\\');
  1001.                 }
  1002.                 else if ((ch = p[1]) >= '0' && ch <= '7')
  1003.                 {
  1004.                     /* up to 3 octal digits */
  1005.                     NEXT_OPEN
  1006.                     NEXTPrintf("%c", '\\');
  1007.                     NEXTPrintf("%c", ch);
  1008.                     ++p;
  1009.                     if ((ch = p[1]) >= '0' && ch <= '7')
  1010.                     {
  1011.                         NEXTPrintf("%c", ch);
  1012.                         ++p;
  1013.                         if ((ch = p[1]) >= '0' && ch <= '7')
  1014.                         {
  1015.                             NEXTPrintf("%c", ch);
  1016.                             ++p;
  1017.                         }
  1018.                     }
  1019.                     break;
  1020.                 }
  1021.     
  1022.                 ++p;
  1023.                 /* just go and print it (fall into the 'default' case) */
  1024.     
  1025.                 /*}}}*/
  1026.             default:
  1027.                 NEXT_OPEN
  1028.     
  1029.                 NEXTPrintf("%c", *p);
  1030.         }
  1031.  
  1032.         /* like TeX, we only do one character in a recursion, unless it's
  1033.          * in braces
  1034.          */
  1035.  
  1036.         if (!brace)
  1037.         {
  1038.             NEXT_FLUSH
  1039.             return(p);  /* the ++p in the outer copy will increment us */
  1040.         }
  1041.     
  1042.     }
  1043.     NEXT_FLUSH
  1044.     return p;
  1045.  
  1046.  
  1047. TERM_PUBLIC void NEXT_put_text(x, y, str)
  1048. unsigned int x, y;
  1049. char *str;
  1050. {
  1051.     /* flush any pending graphics (all the XShow routines do this...) */
  1052.  
  1053.     if (!strlen(str))
  1054.         return;
  1055.  
  1056.     if (NEXT_path_count)
  1057.     {
  1058.         NEXTPrintf(" stroke\n");
  1059.         NEXT_path_count=0;
  1060.         NEXT_relative_ok=FALSE;
  1061.     }
  1062.  
  1063.     NEXT_move(x,y);
  1064.  
  1065.     if (NEXT_ang != 0)
  1066.         NEXTPrintf("currentpoint gsave translate %d rotate 0 0 moveto\n",
  1067.             NEXT_ang*90);
  1068.  
  1069.     NEXTPrintf("[ ");
  1070.  
  1071.     /* set up the globals */
  1072.     
  1073.     NEXT_opened_string = FALSE;
  1074.     NEXT_max_height = -1000;
  1075.     NEXT_min_height = 1000;
  1076.  
  1077.     while (*(str = NEXT_recurse(str, TRUE, NEXT_font,
  1078.                      (double)term->v_char,
  1079.                      0.0, TRUE)));
  1080.  
  1081.     NEXT_max_height += NEXT_min_height;
  1082.         
  1083.     NEXTPrintf( "] %.1f ", -NEXT_max_height/3);
  1084.  
  1085.     switch(NEXT_justify)
  1086.     {
  1087.         case LEFT : NEXTPrintf( "MLshow\n");
  1088.             break;
  1089.         case CENTRE : NEXTPrintf( "MCshow\n");
  1090.             break;
  1091.         case RIGHT : NEXTPrintf( "MRshow\n");
  1092.             break;
  1093.     }
  1094.  
  1095.     if (NEXT_ang != 0)
  1096.         NEXTPrintf("grestore\n");
  1097.     NEXT_path_count = 0;
  1098.     NEXT_relative_ok=FALSE;
  1099. }
  1100.  
  1101.  
  1102.  
  1103.     /* This just packs all the postscript into one (possibly huge) string 
  1104.      * which will be passed (as a fake pointer) via D.O. to the server 
  1105.      */
  1106.  
  1107. void NEXTPrintf(char *fmt, ...)
  1108. {
  1109.     va_list ap;
  1110.     char *pb;
  1111.     int NEXToff;
  1112.  
  1113.                     /* Process formatting instructions */
  1114.     va_start(ap, fmt);
  1115.     vsprintf(NEXTTmpBuf,fmt,ap);
  1116.     va_end(ap);
  1117.  
  1118.                     /* Add to buffer */
  1119.     for (pb=NEXTTmpBuf; (*pb != 0); ++pb, ++NEXTBufAt) {
  1120.                     /* reallocate if necessary */
  1121.         if (NEXTBufAt >= NEXTBufEnd) {
  1122.             NEXToff = NEXTBufAt - NEXTBuffer;
  1123.             NEXTsize *=2;
  1124.             NEXTBuffer = realloc(NEXTBuffer, NEXTsize);
  1125.             NEXTBufEnd = NEXTBuffer + NEXTsize;
  1126.             NEXTBufAt = NEXTBuffer + NEXToff;
  1127.         }
  1128.         *NEXTBufAt = *pb;
  1129.     }
  1130.  
  1131.     return;
  1132.  
  1133. }
  1134.  
  1135. /*  This next section implements the GnuTermDriver object which manages
  1136.     the D.O. connection and interface to OBjC 
  1137. */
  1138.  
  1139.  
  1140. @protocol GnuTermServerMethods
  1141. - (id) executePScode:(char *)PStext;
  1142. @end
  1143.  
  1144.  
  1145. @implementation GnuTermDriver
  1146.  
  1147. - init
  1148. {
  1149.     NXConnection *myConnection;
  1150.     char serverpath[100], *envstring;
  1151.     int GnuTermPathSet=0;
  1152.  
  1153.  
  1154.                             /* Ask OS for connection to server */
  1155.     server = [NXConnection connectToName:"gnuplotServer"];
  1156.  
  1157.                             /* Server is running ready to go */
  1158.     if (server) printf("Connected to server\n");
  1159.  
  1160.                             /* Server isn't running, we must fire it up */
  1161.     else {
  1162.         *serverpath = 0;
  1163.  
  1164.                             /* Check for path set in environment */
  1165.         if ((envstring = getenv("GNUTERMPATH")) != (char *) NULL) {
  1166.             sprintf(serverpath, "%s/GnuTerm.app/GnuTerm", envstring);
  1167.             GnuTermPathSet = 1;
  1168.         }
  1169.  
  1170.                             /* Not in environment */
  1171.         else strcpy(serverpath, "GnuTerm");
  1172.  
  1173.                             /* Try to launch application */
  1174.         if ([[Application workspace] launchApplication:serverpath] == NO) {
  1175.             printf("Failed to launch %s\n.", serverpath);
  1176.                             /* Offer a little advice */
  1177.             if (GnuTermPathSet) {
  1178.                 printf("You must have setenv GNUTERMPATH to something wrong\n");
  1179.                 printf("I recommend you exit gnuplot and fix this.\n");
  1180.             }
  1181.             else {
  1182.                 printf("It must not be located ~/Apps or /LocalApps\n");
  1183.                 printf("I recommend either that you move GnuTerm.app one of these locations\n");
  1184.                 printf("or\n");
  1185.                 printf("host%  setenv GNUTERMPATH /directory/containing/GnuTerm.app\n");
  1186.             }
  1187.         }
  1188.                             /* I wish the gnuplot terminal interface would 
  1189.                                let me return an error here.
  1190.                             */
  1191.  
  1192.                             /* Application is launching */
  1193.         else {
  1194.                             /* Wait for it to register Server methods with OS */
  1195.             do { 
  1196.                 server = [NXConnection connectToName:"gnuplotServer"];
  1197.             } while (!server);        /* This could result in a hang, 
  1198.                                        but I've never seen it fail */
  1199.         }
  1200.     }
  1201.  
  1202.  
  1203.                             /* By limiting ourselves to known protocol
  1204.                              * we speed up the messaging 
  1205.                              */
  1206.     [server setProtocolForProxy:@protocol(GnuTermServerMethods)];
  1207.  
  1208.     myConnection = [server connectionForProxy];
  1209.  
  1210.                             /* If the server dies we want to know about it */
  1211.     [myConnection registerForInvalidationNotification:self];
  1212.  
  1213.     [NXPort worryAboutPortInvalidation];
  1214.  
  1215.     return self;
  1216. }
  1217.  
  1218. - plot:(char *)PSstr;
  1219. {
  1220.                             /* If server has become invalid, re-initialize */
  1221.     if (!server) [self init]; 
  1222.  
  1223.     /* This is where we send the huge postscript string to the server 
  1224.        Note:
  1225.          The D.O. system doesn't actually give this pointer to the server.  
  1226.          The pointer is dereferenced on the client side and the
  1227.          resulting data is sent to the server. On the server side, 
  1228.          space for the data is automatically allocated, and a pointer to
  1229.          the local data is received.
  1230.          For details check out:
  1231. /NextLibrary/Documentation/NextDev/GeneralRef/06_DistributedObjects/IntroDistObjects.rtf
  1232.     */
  1233.  
  1234.     [server executePScode:PSstr];
  1235.  
  1236.     return self;
  1237. }
  1238.  
  1239.                             /* This gets called by OS if server goes down */
  1240. - senderIsInvalid:(id)sender
  1241. {
  1242.     server = 0;
  1243. }
  1244.  
  1245. @end
  1246.  
  1247.  
  1248.  
  1249. #endif /* TERM_BODY */
  1250.  
  1251. #ifdef TERM_TABLE
  1252.  
  1253.        
  1254. TERM_TABLE_START(next_driver)
  1255.     "next", 
  1256.     "Interface to GnuTerm.app under NeXTstep",
  1257.     NEXT_XMAX, NEXT_YMAX, NEXT_VCHAR, NEXT_HCHAR, 
  1258.     NEXT_VTIC, NEXT_HTIC, NEXT_options, NEXT_init, NEXT_reset, 
  1259.     NEXT_text, null_scale, NEXT_graphics, NEXT_move, NEXT_vector, 
  1260.     NEXT_linetype, NEXT_put_text, NEXT_text_angle, 
  1261.     NEXT_justify_text, NEXT_point, do_arrow, NEXT_set_font,
  1262.     NEXT_set_pointsize
  1263.     
  1264.  
  1265. TERM_TABLE_END(next_driver)
  1266.  
  1267. #undef LAST_TERM
  1268. #define LAST_TERM next_driver
  1269.  
  1270. #endif /* TERM_TABLE */
  1271. #endif /* TERM_PROTO_ONLY */
  1272.  
  1273.  
  1274.  
  1275. #ifdef TERM_HELP
  1276. START_HELP(next)
  1277. "1 next",
  1278. "?set terminal next",
  1279. "?next",
  1280. " Several options may be set in the next driver.",
  1281. "",
  1282. " Syntax:",
  1283. "         set terminal next {<mode>} {<color>} {<dashed>} \\",
  1284. "                                 {\"<fontname>\"} {<fontsize>}",
  1285. "",
  1286. " where <mode> is  `default`.",
  1287. " Selecting default sets all options to their defaults.",
  1288. " <color> is either `color` or `monochrome`.",
  1289. " <dashed> is either `solid` or `dashed`.",
  1290. " \"<fontname>\" is the name of a valid PostScript font.",
  1291. " <fontsize> is the size of the font in PostScript points.",
  1292. " Defaults are  `monochrome`, `dashed`, "Helvetica", and 14pt.",
  1293. "",
  1294. " Examples:",
  1295. "",
  1296. "         set term next default       # old postscript",
  1297. "         set term next 22,"
  1298. "         set term next color \"Times-Roman\" 14"
  1299. END_HELP(next)
  1300. #endif
  1301.